<?php

namespace backend\actions;


use backend\component\BaseAction;
use backend\component\ResultFormat;
use yii\base\InvalidArgumentException;

class DisplayAction extends BaseAction
{
    /**
     * 是否展示的字段
     * @var
     */
    public $field = "is_display";

    public function init()
    {
        parent::init();
        if (empty($this->field)){
            throw new InvalidArgumentException("需要配置需要展示的字段");
        }
    }

    public function run($id,$status) {
        if (!$id) {
            return ResultFormat::error("操作失败,缺少参数");
        }
        $sta = $status == 1?0:1;
        $model = $this->modelObj->findOne($id);
        if(!$model->updateAttributes([$this->field=>$sta])) {
            return ResultFormat::error($model->getErrorsToString());
        }
        return ResultFormat::success("操作成功");
    }
}
